﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类ServiceChargePaymodeInfo。
    /// </summary>
    public class ServiceChargePaymodeInfo
    {
        public ServiceChargePaymodeInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_ServiceCharge_PaymodeInfo(");
            strSql.Append("RecordId,ReceiptId,PayMode,PayMoney,PayCode,Memo)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@ReceiptId,@PayMode,@PayMoney,@PayCode,@Memo)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ReceiptId", DbType.Guid, model.ReceiptId);
            db.AddInParameter(dbCommand, "PayMode", DbType.AnsiString, model.PayMode);
            db.AddInParameter(dbCommand, "PayMoney", DbType.Decimal, model.PayMoney);
            db.AddInParameter(dbCommand, "PayCode", DbType.AnsiString, model.PayCode);
            db.AddInParameter(dbCommand, "Memo", DbType.AnsiString, model.Memo);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_ServiceCharge_PaymodeInfo set ");
            strSql.Append("ReceiptId=@ReceiptId,");
            strSql.Append("PayMode=@PayMode,");
            strSql.Append("PayMoney=@PayMoney,");
            strSql.Append("PayCode=@PayCode,");
            strSql.Append("Memo=@Memo");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ReceiptId", DbType.Guid, model.ReceiptId);
            db.AddInParameter(dbCommand, "PayMode", DbType.AnsiString, model.PayMode);
            db.AddInParameter(dbCommand, "PayMoney", DbType.Decimal, model.PayMoney);
            db.AddInParameter(dbCommand, "PayCode", DbType.AnsiString, model.PayCode);
            db.AddInParameter(dbCommand, "Memo", DbType.AnsiString, model.Memo);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_ServiceCharge_PaymodeInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ReceiptId,PayMode,PayMoney,PayCode,Memo from t_ServiceCharge_PaymodeInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ReceiptId,PayMode,PayMoney,PayCode,Memo ");
            strSql.Append(" FROM t_ServiceCharge_PaymodeInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ReceiptId,PayMode,PayMoney,PayCode,Memo ");
            strSql.Append(" FROM t_ServiceCharge_PaymodeInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo> list = new List<Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo model = new Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ReceiptId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiptId = new Guid(ojb.ToString());
            }
            model.PayMode = dataReader["PayMode"].ToString();
            ojb = dataReader["PayMoney"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PayMoney = (decimal)ojb;
            }
            model.PayCode = dataReader["PayCode"].ToString();
            model.Memo = dataReader["Memo"].ToString();
            return model;
        }

        /// <summary>
        /// 获取对象列表
        /// </summary>
        public static DataSet GetListByReceiptId(string ReceiptId)
        {
            string strWhere = string.Format("ReceiptId = '{0}'",ReceiptId);
            return GetList(strWhere);
        }
        #endregion  成员方法
    }
}

